package com.glodon.async.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 模拟远程调用
 * @Author: lilin
 * @Date: 2022/8/10
 */
@Service("remoteService")
public class RemoteService {

    // 模拟用户系统
    private static Map<String, String> USER_INFO = new HashMap<>();
    // 模拟余额系统
    private static Map<String, Integer> USER_CASH = new HashMap<>();
    // 模拟积分系统
    private static Map<String, Integer> USER_AMOUNT = new HashMap<>();
    // 模拟数据库数据
    static {
        USER_INFO.put("10", "张三");
        USER_INFO.put("11", "李四");
        USER_INFO.put("12", "王五");
        USER_INFO.put("13", "赵六");
        USER_INFO.put("14", "小明");
        USER_INFO.put("15", "小红");
        USER_INFO.put("16", "嘎子偷狗");
        USER_INFO.put("17", "潘子卖酒");

        USER_CASH.put("10", 1200);
        USER_CASH.put("11", 1500);
        USER_CASH.put("12", 1800);
        USER_CASH.put("13", 2000);
        USER_CASH.put("14", 3000);
        USER_CASH.put("15", 5000);

        USER_AMOUNT.put("10", 200);
        USER_AMOUNT.put("11", 600);
        USER_AMOUNT.put("12", 800);
        USER_AMOUNT.put("13", 1000);
        USER_AMOUNT.put("14", 300);
        USER_AMOUNT.put("15", 50);
    }


    public JSONObject getUserName(String userId){

        try {
            TimeUnit.MILLISECONDS.sleep(100L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        JSONObject name = new JSONObject();
        name.put("userName", USER_INFO.get(userId));
        name.put("userId", userId);
        return name;
    }


    public JSONObject getUserCash(String userId){

        try {
            TimeUnit.MILLISECONDS.sleep(120);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        JSONObject cash = new JSONObject();
        cash.put("userCash", USER_CASH.get(userId));
        return cash;
    }

    public JSONObject getUserAmount(String userId){

        try {
            TimeUnit.MILLISECONDS.sleep(130L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        JSONObject amount = new JSONObject();
        amount.put("amount", USER_AMOUNT.get(userId));
        return amount;
    }


    /**
     * 模拟批量数据库查询，延迟100毫秒
     * @param paramList 参数list
     */
    public List<JSONObject> getUserNameBath(List<Map<String, Object>> paramList){

        try {
            TimeUnit.MILLISECONDS.sleep(100L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        List<JSONObject> resultList = new ArrayList<>(paramList.size());

        for (Map<String, Object> param : paramList) {
            String userId = (String) param.get("userId");
            String serialNo = (String) param.get("serialNo");

            JSONObject result = new JSONObject();
            String userName = USER_INFO.get(userId);
            result.put("serialNo", serialNo);
            result.put("userName", userName);
            result.put("userId", userId);
            resultList.add(result);
        }

        return resultList;
    }


}
